﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Business_Layer;

namespace BACKOFFICE
{
    public partial class DataUpdateTest : DevExpress.XtraEditors.XtraUserControl
    {
        public static DataUpdateTest _main;
        public DataUpdateTest()
        {
            InitializeComponent();
            _main = this;

            ServerIPValue.Text = MachineInfo.GetJustIP();
        }

        private void DataUpdateTest_Load(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(ServerPortValue.Text))
            {
                MessageBox.Show("Please enter server port.", "Server", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            EstablishRemote();
            ServerPortValue.ReadOnly = true;
            StartServer.Enabled = false;
            //ServerStatusMessage.Text = "Server has been started...";
        }

        private void EstablishRemote()
        {
            try
            {
                SoapServerFormatterSinkProvider soap = new SoapServerFormatterSinkProvider();
                BinaryServerFormatterSinkProvider binary = new BinaryServerFormatterSinkProvider();
                soap.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                binary.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

                soap.Next = binary;

                Hashtable table = new Hashtable();
                table.Add("port", ServerPortValue.Text);

                TcpChannel channel = new TcpChannel(table, null, soap);

                FTPServer.Logger = Logger;

                ChannelServices.RegisterChannel(channel, false);
                RemotingConfiguration.ApplicationName = "FTPServerAPP";
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(FTPServer), "ftpserver.svr", WellKnownObjectMode.Singleton);

                Logger.Text += Environment.NewLine + "***** TCP Channel has been published... *****";
            }
            catch(Exception E)
            {
                Logger.Text += Environment.NewLine + E.Message;
            }

        }
    }
}
